package edu.gatech.cs2340.bytebankers.view;




import java.awt.Font;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.gatech.cs2340.bytebankers.model.Location;
import edu.gatech.cs2340.bytebankers.model.Trail;
import edu.gatech.cs2340.bytebankers.presenter.IMapPresenter;
import edu.gatech.cs2340.bytebankers.presenter.MapPresenter;
import javax.swing.JButton;

public class MapView extends JPanel implements IMapView {
	
	private JLabel mapLabel;
	private IMapPresenter presenter;
	
	private Point[] locationPoints;
	private Point currentPosition;
	private String[] locationNames;
	private final int DIAMETER_OF_WAYPOINTS = 8;
	private final int DIAMETER_OF_CURRENT_POINT = 16;
	public JButton btnBack;
	
	public MapView(IMapPresenter presenter){
		this.presenter = presenter;
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ButtonListener());
		add(btnBack);
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		
		
		drawLines(g);
		drawWayPoints(g);
		drawText(g);
		drawCurrentPoint(g);
	}

	@Override
	public void onBackToJourneyClick() {
		presenter.onBackToJourneyClick();
	}
	
	private void drawLines(Graphics g) {
		for(int i = 0; i < locationPoints.length - 1; i++) {
			Point currentPoint = locationPoints[i];
			Point nextPoint = locationPoints[i+1];
			g.drawLine(currentPoint.x, currentPoint.y, nextPoint.x, nextPoint.y);
		}
	}
	
	private void drawText(Graphics g) {
		 Graphics2D g2d = (Graphics2D)g;
		    
		    Font font = new Font("Arial", Font.PLAIN, 9);
		    
		    g2d.setFont(font);
		    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		for(int i = 0; i < locationPoints.length; i++) {
			Point currentPoint = locationPoints[i];
			g2d.setColor(new Color(0,0,0));
			g2d.drawString(locationNames[i], currentPoint.x, currentPoint.y - 5);
		}
	}
	
	private void drawCurrentPoint(Graphics g) {
		g.setColor(Color.RED);
		g.fillOval(currentPosition.x - DIAMETER_OF_CURRENT_POINT/2, 
				currentPosition.y - DIAMETER_OF_CURRENT_POINT/2,
				DIAMETER_OF_CURRENT_POINT,
				DIAMETER_OF_CURRENT_POINT);

	}

	@Override
	public void setCurrentPosition(Point position) {
		this.currentPosition = position;
	}

	@Override
	public void setLocationNames(String[] locationNames) {
		this.locationNames = locationNames;
	}

	@Override
	public void setLocationPoints(Point[] locationPoints) {
		this.locationPoints = locationPoints;
	}

	@Override
	public JPanel getMainPanel() {
		return this;
	}
	
	/**
	 * THis method draws tiny cirles on the panel to indicate a waypoint.
	 */
	public void drawWayPoints(Graphics g){
		for(int i = 0; i < locationPoints.length; i++){
			g.setColor(Color.green);
			g.fillOval((int)(locationPoints[i].getX() - DIAMETER_OF_WAYPOINTS/2),
					(int)(locationPoints[i].getY() - DIAMETER_OF_WAYPOINTS/2),
					DIAMETER_OF_WAYPOINTS,
					DIAMETER_OF_WAYPOINTS);
		}
	}
	
	 private class ButtonListener implements ActionListener {
			public void actionPerformed(ActionEvent event) {
			    if (event.getSource() == btnBack) {
			    	onBackToJourneyClick();
			    }
			}
		    }

}
